package io.renren.modules.shop.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.utils.R;
import io.renren.modules.shop.entity.CustomerEntity;
import io.renren.modules.shop.entity.MerchantEntity;
import io.renren.modules.shop.entity.UsersEntity;
import io.renren.modules.shop.service.CustomerService;
import io.renren.modules.shop.service.MerchantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/shop")
public class LoginController {

    @Autowired
    CustomerService customerService;
    @Autowired
    MerchantService merchantService;

    /**
     * 登录
     */
    @RequestMapping("/cLogin")
    public R CLogin(@RequestBody UsersEntity user){

        QueryWrapper<CustomerEntity> queryWrapper = new QueryWrapper<CustomerEntity>();
        queryWrapper.lambda().eq(CustomerEntity::getMobilePhone,user.getUserName());
        //用户信息
        CustomerEntity customer = customerService.getOne(queryWrapper);

        //账号不存在、密码错误
        if(customer==null || !customer.getPassword().equals(user.getPassWord())) {
            return R.error("账号或密码不正确");
        }
        //账号锁定
        if(customer.getUserStats() == 0){
            return R.error("账号已被锁定,请联系管理员");
        }
        return R.ok();
    }

    @RequestMapping("/mLogin")
    public R MLogin(@RequestBody UsersEntity user){

        QueryWrapper<MerchantEntity> queryWrapper = new QueryWrapper<MerchantEntity>();
        queryWrapper.lambda().eq(MerchantEntity::getMobilePhone,user.getUserName());
        //用户信息
        MerchantEntity merchant = merchantService.getOne(queryWrapper);

        //账号不存在、密码错误
        if(merchant==null || !merchant.getPassword().equals(user.getPassWord())) {
            return R.error("账号或密码不正确");
        }
        return R.ok();
    }
}
